﻿/*
 * Author: Tomasz Poradowski
 * 2007-03-14
 *
 * Copyright (c) 2007, Poradowski.com
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification, are
 * permitted provided that the following conditions are met:
 * - Redistributions of source code must retain the above copyright notice, this list 
 *   of conditions and the following disclaimer.
 * - Redistributions in binary form must reproduce the above copyright notice, this list
 *   of conditions and the following disclaimer in the documentation and/or other materials 
 *   provided with the distribution.
 * - Neither the name of the Poradowski.com nor the names of its contributors may be used to 
 *   endorse or promote products derived from this software without specific prior written 
 *   permission.
 * 
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS &AS IS& AND ANY EXPRESS 
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY 
 * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 
 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 
 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 * $Id: Main.cs 5 2007-03-15 17:55:34Z tomasz.poradowski $
 */

using System;
using System.Collections.Generic;
using System.IO;
using System.Diagnostics;

namespace CSVMerger
{
    /// <summary>
    /// Tool's main class.
    /// </summary>
	class Merger
	{
	    /// <summary>
	    /// When in debug mode - redirect messages to this file.
	    /// </summary>
	    private const string DEBUG_FILE = "csvmerger.log";

	    /// <summary>
	    /// Application starting point.
	    /// </summary>
	    /// <param name="args">program command-line arguments</param>
	    /// <returns>0 when sucessfull</returns>
		public static int Main(string[] args)
		{
		    initDebug();

		    string configFile = null;

		    //check for parameter - configuration file name
		    if (args.Length > 0)
		        configFile = args[0];
		    if ((configFile == null) || !File.Exists(configFile)) {
		        Console.Error.WriteLine("Usage: CSVMerger <configuration.xml>");
			    Debug.WriteLine("[" + DateTime.Now + "] END (-1)");
		        return (-1);
		    }

		    //do the work
		    CSVMerger csvMerger = new CSVMerger(configFile);
		    try {
		        csvMerger.merge();
		    } catch (Exception exc) {
		        Console.Error.WriteLine("Error: " + exc.Message);
		        Debug.WriteLine(exc.ToString());
			    Debug.WriteLine("[" + DateTime.Now + "] END (-2)");
		        return -2;
		    }

		    Debug.WriteLine("[" + DateTime.Now + "] END");
		    return 0;
		}

		/// <summary>
		/// Initialization of debugging redirection to a file. Gets compiled
		/// in when "DEBUG" conditional is set.
		/// </summary>
		[Conditional("DEBUG")]
		private static void initDebug() {
            try {
	    		Debug.Listeners.Clear();
		    	Debug.Listeners.Add(new TextWriterTraceListener(DEBUG_FILE));
			    Debug.AutoFlush = true;
			    Debug.WriteLine("[" + DateTime.Now + "] START");
            } catch {
            }
		}
	}
}
